// packageI/pay_back/pay_back.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagUrl: app.globalData.imgPath,
    Interval: null,
    showPacket: false,
    name: '',
    is_cps: '',
    order_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '跳转中',
    });
    console.log(options);
    if (options.is_cps) {
      this.setData({
        is_cps: options.is_cps
      });
    }
    if (options.name) {
      this.setData({
        name: options.name
      });
    }
    if (options.ids) {
      this.setData({
        order_id: options.ids
      });
    }
    if (options.sn) {
      this.setData({
        order_sn: options.sn
      });
    }
    this.getData();
  },
  toPacket() {
    wx.navigateTo({
      url: "/packageE/ConsumePacket/ConsumePacketSuccess/ConsumePacketSuccess?order_id=" + this.data.order_id.toString(),
    });
  },
  onClickHide() {
    this.setData({
      showPacket: false,
    });
    this.toRedirect(this.data.redirect);
  },
  getData() {
    let query = {};
    let api = '';
    if(this.data.name == 'tourAround') {
      api = 'plugin.travel-around.frontend.payment.payment.checkOrderPay';
      query = {
        order_id: this.data.order_id
      };
    } else {
      api = 'order.merge-pay.check-order-pay';
      query = {
        order_pay_id: this.data.order_id
      };
    }
    let urlStr = app.getNetAddresss(api);
    app._postNetWork({
      url: urlStr,
      data: query,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          if (res.data.status == 1) {
            this.setData({
              redirect: res.data.redirect
            });
            this.checkoutConsumePacket(res.data.redirect);
            clearInterval(this.data.Interval);
            this.data.Interval = null;
          } else {
            if (!this.data.Interval) {
              this.data.Interval = setInterval(() => {
                this.getData();
              }, 1000);
            }
          }
        } else {
          app.tips(res.msg);
        }
      },
      fail: function (res) {}
    });
  },
  checkoutConsumePacket(redirect) {
    let basicinfo = wx.getStorageSync("yz_basic_info");
    if (basicinfo && basicinfo.globalParameter.consume_red_packet_status == 1) {
      let urlStr = app.getNetAddresss("plugin.consume-red-packet.Frontend.Modules.Packet.Controllers.has.index");
      app._postNetWork({
        url: urlStr,
        data: {
          order_id: this.data.order_id.split(","),
        },
        success: (resdata) => {
          var res = resdata.data;
          if (res.result == 1) {
            this.setData({
              showPacket: true,
            });
          } else {
            this.toRedirect(redirect);
          }
        },
        fail: (res) => {
          this.toRedirect(redirect);
        },
      });
    } else {
      this.toRedirect(redirect);
    }
  },
  toRedirect(redirect) {
    console.log(redirect);
    if (this.data.is_cps) {
      wx.navigateTo({
        url: "/packageA/member/order/cpsCoupon/cpsCoupon",
      });
      /** #92329 有一些卡券订单是没有卡密的 统一跳转到卡券订单 */
      return;
    }
    if (this.data.name == "Myshaky") {
      wx.navigateTo({
        url: "/packageC/Myshaky/activityHome/activityHome",
      });
      return;
    }
    console.log(this.options)
    if(this.options.plugName){
      if(this.options.plugName == 'assessment_record'){
        wx.navigateTo({
          url: `/mircoApp/assessment/assessmentReport/assessmentReport?id=${this.options.ass_log_id}&view=true`,
        })
      } else if (this.options.plugName == 'assessment'){
        wx.navigateTo({
          url: `/mircoApp/assessment/assessmentReport/assessmentReport?id=${this.options.ass_log_id}`,
        })
      }
      return
    }
    // if (this.data.name == "blindBox") {
    //   // 跳转去订单详情 this.data.orderDetail_id
    //   // wx.redirectTo({
    //   //   url: "/packageA/member/orderdetail/orderdetail?order_id=" + this.data.orderDetail_id + "&orderType=shop",
    //   // });
    //   return;
    // }
    if (this.data.order_sn||!app._isTextEmpty(redirect)) {
      this.payCallBack(redirect);
    }
  },
  payCallBack(redirect) {
    console.log(redirect);
    let URL = '';
    if (app.globalData.store_alone_temp == 1) {
      // 开启了门店独立直接跳首页
      wx.reLaunch({
        url: '/packageG/independent_store/index/index?store_id=' + app.globalData.STORE_ID,
      });
      return;
    }
    if (app._isTextEmpty(redirect)) {
      wx.reLaunch({
        url: "/packageG/index/index",
      });
    } else {
      try {
        if ((this.data.name == "groups"||this.data.name == "ywmGroup") && redirect.indexOf("group_detail") != -1) {
          // 拼团
          let str = redirect.match(/group_detail\/(\S*)\?i/)[1];
          str = str.split("/");
          let id = str[0];

          URL = "/packageB/member/group/GroupDetail/GroupDetail?id=" + id;
          if(this.data.name == "ywmGroup") URL = URL + '&plu=ywm';

          if (str[1]) {
            wx.redirectTo({
              url: URL + "&store_id=" + str[1],
            });
          } else {
            wx.redirectTo({
              url: URL
            });
          }
        } else {
          wx.redirectTo({
            url: redirect,
            fail: (err) => {
              console.log(err);
              wx.reLaunch({
                url: "/packageG/index/index",
              });
            },
          });
        }
      } catch (e) {
        wx.reLaunch({
          url: "/packageG/index/index",
        });
      }
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.Interval);
    this.data.Interval = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.Interval);
    this.data.Interval = null;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});